//
//  TurntableVC.m
//  Turntable
//
//  Created by yaqiang bi on 2017/3/10.
//  Copyright © 2017年 yaqiang bi. All rights reserved.
//

#import "TurntableVC.h"
#define AnimationDuration 0.5
#define screenHeight [[UIScreen mainScreen] bounds].size.height                 //屏幕宽度
#define screenWidth [[UIScreen mainScreen] bounds].size.width                   //屏幕高度
@interface TurntableVC ()<CAAnimationDelegate>{
    BOOL _isCompleteAnimation;
    BOOL _isRequest;
    NSInteger duration;
}
@property (nonatomic,strong)UIImageView *turnTable;
@property (nonatomic,strong)UIImageView *arrow;
@property (nonatomic,strong)UIImageView *wheel;
@property (nonatomic, strong) CADisplayLink *link;
@end

@implementation TurntableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initViews];
}
#pragma -mark 界面搭建
-(void)initViews{
    UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    back.image = [UIImage imageNamed:@"back"];
    back.userInteractionEnabled = YES;
    [self.view addSubview:back];

    self.turnTable = [[UIImageView alloc]initWithFrame:CGRectMake(0, (screenHeight-screenWidth)/2.0, screenWidth, screenWidth)];
    self.turnTable.image = [UIImage imageNamed:@"turnTable"];
    self.turnTable.userInteractionEnabled = YES;
    [back addSubview:_turnTable];

    UIImage *arrowImage = [UIImage imageNamed:@"arrow"];
    CGFloat width = screenWidth*0.3;
    CGFloat height = arrowImage.size.height/arrowImage.size.width*width;
    self.arrow = [[UIImageView alloc]initWithFrame:CGRectMake((screenWidth - width)/2.0, (screenWidth-height)/2.0, width, height)];
    self.arrow.image = arrowImage;
    self.arrow.userInteractionEnabled = YES;
    [_turnTable addSubview:_arrow];
    
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    startBtn.frame = CGRectMake(0, screenHeight-100, screenWidth/2.0, 50);
    [startBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(draw:) forControlEvents:UIControlEventTouchUpInside];
    [startBtn setTitle:@"开始" forState:UIControlStateNormal];
    [self.view addSubview:startBtn];
    
    UIButton *endBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    endBtn.frame = CGRectMake(screenWidth/2.0, screenHeight-100, screenWidth/2.0, 50);
    [endBtn addTarget:self action:@selector(stopRotate) forControlEvents:UIControlEventTouchUpInside];
    [endBtn setTitle:@"结束" forState:UIControlStateNormal];
    [endBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:endBtn];
}
#pragma -mark Action
-(void)draw:(UIButton*)sender{
    if (_isRequest) {
        return;
    }
    _isRequest = YES;
    [self.arrow.layer removeAllAnimations];
    self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(firmlyAnimation)];
    self.link.preferredFramesPerSecond = 1/AnimationDuration;
    [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
//抽奖结果未返回的动画
-(void)firmlyAnimation{
    //self.arrow.transform = CGAffineTransformRotate(self.arrow.transform, 2*M_PI/12);
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation";
    animation.duration = AnimationDuration;
    animation.fromValue = @0;
    animation.toValue = @(M_PI*2);
    animation.delegate = self;
    animation.repeatCount = 1;
    [self.arrow.layer addAnimation:animation forKey:@"rotate"];
}
-(void)stopRotate{
    _isRequest = NO;
    [self.link invalidate];
    self.link = nil;
    _isCompleteAnimation = YES;
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if (_isCompleteAnimation) {
        _isCompleteAnimation = NO;
        int random = arc4random()%8+1;
        CGFloat time = (M_PI*2+M_PI*2/8*random)/(M_PI/AnimationDuration);
        CABasicAnimation *animation = [CABasicAnimation animation];
        animation.keyPath = @"transform.rotation";
        animation.duration = time;
        animation.fromValue = @0;
        animation.toValue = @(M_PI*2+M_PI*2/8*random);
        animation.delegate = self;
        animation.repeatCount = 1;
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeForwards;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        [self.arrow.layer addAnimation:animation forKey:@"rotateAnimation"];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
